package.path = "./plugin/CT16S48V/utils.lua"..";..\\?.lua"
package.path = "./plugin/CT16S48V/json.lua"..";..\\?.lua"

require "utils.lua"
require "json.lua"

rxBuf = {}
regAddr = 0x0000

function NewVariables()
    -- body
    local VariableMap = {
        Variable = {
        },
    }

    return VariableMap
end

function GenerateGetRealVariables(sAddr,step)

    -- body
    local requestADU = {}
    local requestADULen = 0
    local requestPDU = {}
    local requestPDUStr = {}
    local VariableMap = {
        Status = "0",
        Variable = {},
    }

    if(step == 0)
    then
        ----------读运行参数-----------------
        requestPDU[1] = 0x22
        requestPDU[2] = sAddr
        requestPDU[3] = 0x4A
        requestPDU[4] = 0x42
        requestPDU[5] = 0xE0
        requestPDU[6] = 0x02
        requestPDU[7] = 0x01

        requestPDUStr = utils.Hex2Str(requestPDU,7)
        local chksum = utils.GetCHKSUM(requestPDUStr,14)

        table.insert(requestADU,0x7E)
        requestADULen = requestADULen + 1
        for index=1,14,1 do
            table.insert(requestADU,requestPDUStr[index])
        end
        requestADULen = requestADULen + 14
        for index=1,4,1 do
            table.insert(requestADU,chksum[index])
        end
        requestADULen = requestADULen + 4
        table.insert(requestADU,0x0D)
        requestADULen = requestADULen + 1

        regAddr = 0x0000
        VariableMap = {
            Status = "0",
            Variable = requestADU
        }
    end

    return VariableMap
end

function DeviceCustomCmd(sAddr,cmdName,cmdParam,step)

    -- body
    local requestADU = {}
    local requestADULen = 0
    local requestPDU = {}
    local requestPDUStr = {}
    local VariableMap = {
        Status = "0",
        Variable = {},
    }

    if(cmdName == "GetRealVariables")
    then
        if(step == 0)
        then
            ----------读运行参数-----------------
            requestPDU[1] = 0x22
            requestPDU[2] = sAddr
            requestPDU[3] = 0x4A
            requestPDU[4] = 0x42
            requestPDU[5] = 0xE0
            requestPDU[6] = 0x02
            requestPDU[7] = 0x01

            requestPDUStr = utils.Hex2Str(requestPDU,7)
            local chksum = utils.GetCHKSUM(requestPDUStr,14)

            table.insert(requestADU,0x7E)
            requestADULen = requestADULen + 1
            for index=1,14,1 do
                table.insert(requestADU,requestPDUStr[index])
            end
            requestADULen = requestADULen + 14
            for index=1,4,1 do
                table.insert(requestADU,chksum[index])
            end
            requestADULen = requestADULen + 4
            table.insert(requestADU,0x0D)
            requestADULen = requestADULen + 1

            regAddr = 0x0000
            VariableMap = {
                Status = "0",
                Variable = requestADU
            }
        end        
    end

    return VariableMap
end

function AnalysisRx(sAddr,rxBufCnt)

    local SOI,VER,ADR,CID1,CID2,LENGTHTmp,LENGTH,COMMAND,DATA,CHKSUM,EOI

    -- body
    local VariableMap = {
        Status = "1",
        Variable = {},
    }

    if(rxBufCnt < 11)
    then
        rxBuf = {}
        return VariableMap
    end

    local tmp = {}
    for index=1,rxBufCnt,1 do
        if(index <= rxBufCnt)
        then
            SOI  = rxBuf[index]
            if(SOI == 0x7E) then    
                VER  = utils.Str2Hex(utils.Copy(rxBuf,index+1,2),2)
                ADR  = utils.Str2Hex(utils.Copy(rxBuf,index+3,2),2)
                CID1 = utils.Str2Hex(utils.Copy(rxBuf,index+5,2),2)
                CID2 = utils.Str2Hex(utils.Copy(rxBuf,index+7,2),2)
                LENGTHTmp = utils.Str2Hex(utils.Copy(rxBuf,index+9,4),4)
                COMMAND = utils.Str2Hex(utils.Copy(rxBuf,index+13,2),2)
                local DATAIndex = 15

                print("VER "..string.format("%02X",VER[1]))
                print("ADR "..string.format("%02X",ADR[1]))
                print("CID1 "..string.format("%02X",CID1[1]))
                print("CID2 "..string.format("%02X",CID2[1]))
                print("LENGTH "..string.format("%02X%02X",LENGTHTmp[1],LENGTHTmp[2]))
                print("COMMAND "..string.format("%02X",COMMAND[1]))

                --判断设备标识码和控制标识码
                if(CID1[1]==0x4A and CID2[1]==0x00) then
                    local Variable = {}
                    local VariableIndex = 0
                    --获取第1组电池遥测数据
                    if(COMMAND[1] == 0x01) then            
                        if(rxBufCnt < 190) then
                            rxBuf = {}
                            return VariableMap
                        end

                        local tmp
                        -------------------------------------------------------------------------------
                        tmp = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,4),4)
                        local SOC = tmp[1]*256+tmp[2]
                        DATAIndex = DATAIndex + 4
                        Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"SOC",SOC,SOC)
                        VariableIndex = VariableIndex + 1
                        print("SOC "..string.format("%d",SOC))
                        -------------------------------------------------------------------------------
                        tmp = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,4),4)
                        local TotalVol = tmp[1]*256+tmp[2]
                        DATAIndex = DATAIndex + 4
                        Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"TotalVol",TotalVol,TotalVol)
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        tmp = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,2),2)
                        local BatteryCnt = tmp[1]
                        DATAIndex = DATAIndex + 2
                        Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"BatteryCnt",BatteryCnt,BatteryCnt)
                        VariableIndex = VariableIndex + 1
                        -----------------------------------------------------------------------------
                        tmp = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,4),4)
                        local BatteryVol01 = tmp[1]*256+tmp[2]
                        DATAIndex = DATAIndex + 4
                        Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"BatteryVol01",BatteryVol01,BatteryVol01)
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        tmp = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,4),4)
                        local BatteryVol02 = tmp[1]*256+tmp[2]
                        DATAIndex = DATAIndex + 4
                        Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"BatteryVol02",BatteryVol02,BatteryVol02)
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        tmp = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,4),4)
                        local BatteryVol03 = tmp[1]*256+tmp[2]
                        DATAIndex = DATAIndex + 4
                        Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"BatteryVol03",BatteryVol03,BatteryVol03)
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        tmp = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,4),4)
                        local BatteryVol04 = tmp[1]*256+tmp[2]
                        DATAIndex = DATAIndex + 4
                        Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"BatteryVol04",BatteryVol04,BatteryVol04)
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        tmp = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,4),4)
                        local BatteryVol05 = tmp[1]*256+tmp[2]
                        DATAIndex = DATAIndex + 4
                        Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"BatteryVol05",BatteryVol05,BatteryVol05)
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        tmp = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,4),4)
                        local BatteryVol06 = tmp[1]*256+tmp[2]
                        DATAIndex = DATAIndex + 4
                        Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"BatteryVol06",BatteryVol06,BatteryVol06)
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        tmp = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,4),4)
                        local BatteryVol07 = tmp[1]*256+tmp[2]
                        DATAIndex = DATAIndex + 4
                        Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"BatteryVol07",BatteryVol07,BatteryVol07)
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        tmp = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,4),4)
                        local BatteryVol08 = tmp[1]*256+tmp[2]
                        DATAIndex = DATAIndex + 4
                        Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"BatteryVol08",BatteryVol08,BatteryVol08)
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        tmp = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,4),4)
                        local BatteryVol09 = tmp[1]*256+tmp[2]
                        DATAIndex = DATAIndex + 4
                        Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"BatteryVol09",BatteryVol09,BatteryVol09)
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        tmp = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,4),4)
                        local BatteryVol10 = tmp[1]*256+tmp[2]
                        DATAIndex = DATAIndex + 4
                        Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"BatteryVol10",BatteryVol10,BatteryVol10)
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        tmp = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,4),4)
                        local BatteryVol11 = tmp[1]*256+tmp[2]
                        DATAIndex = DATAIndex + 4
                        Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"BatteryVol11",BatteryVol11,BatteryVol11)
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        tmp = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,4),4)
                        local BatteryVol12 = tmp[1]*256+tmp[2]
                        DATAIndex = DATAIndex + 4
                        Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"BatteryVol12",BatteryVol12,BatteryVol12)
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        tmp = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,4),4)
                        local BatteryVol13 = tmp[1]*256+tmp[2]
                        DATAIndex = DATAIndex + 4
                        Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"BatteryVol13",BatteryVol13,BatteryVol13)
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        tmp = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,4),4)
                        local BatteryVol14 = tmp[1]*256+tmp[2]
                        DATAIndex = DATAIndex + 4
                        Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"BatteryVol14",BatteryVol14,BatteryVol14)
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        tmp = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,4),4)
                        local BatteryVol15 = tmp[1]*256+tmp[2]
                        DATAIndex = DATAIndex + 4
                        Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"BatteryVol15",BatteryVol15,BatteryVol15)
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        tmp = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,4),4)
                        local BatteryVol16 = tmp[1]*256+tmp[2]
                        DATAIndex = DATAIndex + 4
                        Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"BatteryVol16",BatteryVol16,BatteryVol16)
                        VariableIndex = VariableIndex + 1
                        -----------------------------------------------------------------------------
                        tmp = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,4),4)
                        local Temperature = tmp[1]*256+tmp[2]
                        DATAIndex = DATAIndex + 4
                        Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"Temperature",Temperature,Temperature)
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        tmp = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,4),4)
                        local BatteryAvrTemp = tmp[1]*256+tmp[2]
                        DATAIndex = DATAIndex + 4
                        Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"BatteryAvrTemp",BatteryAvrTemp,BatteryAvrTemp)
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        tmp = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,4),4)
                        local MOSTemp = tmp[1]*256+tmp[2]
                        DATAIndex = DATAIndex + 4
                        Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"MOSTemp",MOSTemp,MOSTemp)
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        tmp = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,2),2)
                        local BatteryTempCnt = tmp[1]
                        DATAIndex = DATAIndex + 2
                        Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"BatteryTempCnt",BatteryTempCnt,BatteryTempCnt)
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        tmp = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,4),4)
                        local BatteryTemp01 = tmp[1]*256+tmp[2]
                        DATAIndex = DATAIndex + 4
                        Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"BatteryTemp01",BatteryTemp01,BatteryTemp01)
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        tmp = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,4),4)
                        local BatteryTemp02 = tmp[1]*256+tmp[2]
                        DATAIndex = DATAIndex + 4
                        Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"BatteryTemp02",BatteryTemp02,BatteryTemp02)
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        tmp = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,4),4)
                        local BatteryTemp03 = tmp[1]*256+tmp[2]
                        DATAIndex = DATAIndex + 4
                        Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"BatteryTemp03",BatteryTemp03,BatteryTemp03)
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        tmp = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,4),4)
                        local BatteryTemp04 = tmp[1]*256+tmp[2]
                        DATAIndex = DATAIndex + 4
                        Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"BatteryTemp04",BatteryTemp04,BatteryTemp04)
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        tmp = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,4),4)
                        local Current = tmp[1]*256+tmp[2]
                        DATAIndex = DATAIndex + 4
                        Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"Current",Current,Current)
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        tmp = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,4),4)
                        local BatteryRegister = tmp[1]*256+tmp[2]
                        DATAIndex = DATAIndex + 4
                        Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"BatteryRegister",BatteryRegister,BatteryRegister)
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        tmp = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,4),4)
                        local SOH = tmp[1]*256+tmp[2]
                        DATAIndex = DATAIndex + 4
                        Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"SOH",SOH,SOH)
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        tmp = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,2),2)
                        local UserCnt = tmp[1]
                        DATAIndex = DATAIndex + 2
                        Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"UserCnt",UserCnt,UserCnt)
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        tmp = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,4),4)
                        local FullCap = tmp[1]*256+tmp[2]
                        DATAIndex = DATAIndex + 4
                        Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"FullCap",FullCap,FullCap)
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        tmp = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,4),4)
                        local SurplusCap = tmp[1]*256+tmp[2]
                        DATAIndex = DATAIndex + 4
                        Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"SurplusCap",SurplusCap,SurplusCap)
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        tmp = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,4),4)
                        local CycleIndex = tmp[1]*256+tmp[2]
                        DATAIndex = DATAIndex + 4
                        Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"CycleIndex",CycleIndex,CycleIndex)
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        tmp = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,4),4)
                        local VoltateState = tmp[1]*256+tmp[2]
                        DATAIndex = DATAIndex + 4
                        local VoltateStateB2 = utils.And(VoltateState,0x0004)
                        if(VoltateStateB2 == 0) then
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"VoltateStateB2",0,"0")
                        else 
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"VoltateStateB2",1,"1")
                        end 
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        local VoltateStateB3 = utils.And(VoltateState,0x0008)
                        if(VoltateStateB3 == 0) then
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"VoltateStateB3",0,"0")
                        else 
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"VoltateStateB3",1,"1")
                        end 
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        local VoltateStateB6 = utils.And(VoltateState,0x0040)
                        if(VoltateStateB6 == 0) then
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"VoltateStateB6",0,"0")
                        else 
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"VoltateStateB6",1,"1")
                        end 
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        local VoltateStateB7 = utils.And(VoltateState,0x0080)
                        if(VoltateStateB7 == 0) then
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"VoltateStateB7",0,"0")
                        else 
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"VoltateStateB7",1,"1")
                        end 
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        local VoltateStateB8 = utils.And(VoltateState,0x0100)
                        if(VoltateStateB8 == 0) then
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"VoltateStateB8",0,"0")
                        else 
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"VoltateStateB8",1,"1")
                        end 
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        tmp = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,4),4)
                        local CurrentState = tmp[1]*256+tmp[2]
                        DATAIndex = DATAIndex + 4
                        local CurrentStateB0 = utils.And(CurrentState,0x0001)
                        if(CurrentStateB0 == 0) then
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"CurrentStateB0",0,"0")
                        else 
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"CurrentStateB0",1,"1")
                        end 
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        local CurrentStateB1 = utils.And(CurrentState,0x0002)
                        if(CurrentStateB1 == 0) then
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"CurrentStateB1",0,"0")
                        else 
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"CurrentStateB1",1,"1")
                        end 
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        local CurrentStateB2 = utils.And(CurrentState,0x0004)
                        if(CurrentStateB2 == 0) then
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"CurrentStateB2",0,"0")
                        else 
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"CurrentStateB2",1,"1")
                        end 
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        local CurrentStateB3 = utils.And(CurrentState,0x0008)
                        if(CurrentStateB3 == 0) then
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"CurrentStateB3",0,"0")
                        else 
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"CurrentStateB3",1,"1")
                        end 
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        local CurrentStateB6 = utils.And(CurrentState,0x0040)
                        if(CurrentStateB6 == 0) then
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"CurrentStateB6",0,"0")
                        else 
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"CurrentStateB6",1,"1")
                        end 
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        local CurrentStateB7 = utils.And(CurrentState,0x0080)
                        if(CurrentStateB7 == 0) then
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"CurrentStateB7",0,"0")
                        else 
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"CurrentStateB7",1,"1")
                        end 
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        tmp = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,4),4)
                        local TempState = tmp[1]*256+tmp[2]
                        DATAIndex = DATAIndex + 4
                        local TempStateB8 = utils.And(TempState,0x0100)
                        if(TempStateB8 == 0) then
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"TempStateB8",0,"0")
                        else 
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"TempStateB8",1,"1")
                        end 
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        local TempStateB9 = utils.And(TempState,0x0200)
                        if(TempStateB9 == 0) then
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"TempStateB9",0,"0")
                        else 
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"TempStateB9",1,"1")
                        end 
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        local TempStateB10 = utils.And(TempState,0x0400)
                        if(TempStateB10 == 0) then
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"TempStateB10",0,"0")
                        else 
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"TempStateB10",1,"1")
                        end 
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        local TempStateB11 = utils.And(TempState,0x0800)
                        if(TempStateB11 == 0) then
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"TempStateB11",0,"0")
                        else 
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"TempStateB11",1,"1")
                        end 
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        local TempStateB12 = utils.And(TempState,0x0800)
                        if(TempStateB12 == 0) then
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"TempStateB12",0,"0")
                        else 
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"TempStateB12",1,"1")
                        end 
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        local TempStateB13 = utils.And(TempState,0x1000)
                        if(TempStateB13 == 0) then
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"TempStateB13",0,"0")
                        else 
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"TempStateB13",1,"1")
                        end 
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        tmp = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,4),4)
                        local AlarmState = tmp[1]*256+tmp[2]
                        DATAIndex = DATAIndex + 4
                        local AlarmStateB7 = utils.And(AlarmState,0x0080)
                        if(AlarmStateB7 == 0) then
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"AlarmStateB7",0,"0")
                        else 
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"AlarmStateB7",1,"1")
                        end 
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        local AlarmStateB11 = utils.And(AlarmState,0x0800)
                        if(AlarmStateB11 == 0) then
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"AlarmStateB11",0,"0")
                        else 
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"AlarmStateB11",1,"1")
                        end 
                        VariableIndex = VariableIndex + 1
                        local AlarmStateB12 = utils.And(AlarmState,0x1000)
                        if(AlarmStateB12 == 0) then
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"AlarmStateB12",0,"0")
                        else 
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"AlarmStateB12",1,"1")
                        end 
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        tmp = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,4),4)
                        local FetState = tmp[1]*256+tmp[2]
                        DATAIndex = DATAIndex + 4
                        Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"FetState",FetState,FetState)
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        tmp = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,4),4)
                        local OverVoltateState = tmp[1]*256+tmp[2]
                        DATAIndex = DATAIndex + 4
                        Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"OverVoltateState",OverVoltateState,OverVoltateState)
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        tmp = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,4),4)
                        local UnderVoltateState = tmp[1]*256+tmp[2]
                        DATAIndex = DATAIndex + 4
                        Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"UnderVoltateState",UnderVoltateState,UnderVoltateState)
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        tmp = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,4),4)
                        local HighVoltateState = tmp[1]*256+tmp[2]
                        DATAIndex = DATAIndex + 4
                        Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"HighVoltateState",HighVoltateState,HighVoltateState)
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        tmp = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,4),4)
                        local LowVoltateState = tmp[1]*256+tmp[2]
                        DATAIndex = DATAIndex + 4
                        Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"LowVoltateState",LowVoltateState,LowVoltateState)
                        VariableIndex = VariableIndex + 1
                        -------------------------------------------------------------------------------
                        tmp = utils.Str2Hex(utils.Copy(rxBuf,index+DATAIndex,4),4)
                        local EquilibriumState = tmp[1]*256+tmp[2]
                        DATAIndex = DATAIndex + 4
                        Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"EquilibriumState",EquilibriumState,EquilibriumState)
                        VariableIndex = VariableIndex + 1
                        -----------------------------------------------------------------------------         
                        VariableMap = {
                            Status = "0",
                            Variable = Variable
                        }
                        rxBuf = {}
                        return VariableMap         
                    end                 
                end
            else
                index = index + 1
            end
        else
            rxBuf = {}
            return VariableMap
        end
    end

    rxBuf = {}
    return VariableMap
end
